package com.bening.smsapp.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.*;
import oracle.jdbc.*;
import com.bening.smsapp.bean.Asn1Bean;
import com.bening.smsapp.bean.FixedLengthAsciiBean;
import com.bening.smsapp.bootstrap.BootConfigParameters;
import com.bening.smsapp.constant.PropertiesConstant;
import com.bening.smsapp.factory.SmsAppFactory;

public class SmsAppDaoImpl implements SmsAppDao {

	private static BootConfigParameters bootParams = BootConfigParameters.getInstance();
	final Logger logger = Logger.getLogger( SmsAppDaoImpl.class );
	
	public List getFileList(int flagType) {
		// TODO Auto-generated method stub
		return null;
	}
	public void updateFileListFlag(int updateTo, List fileList) {
		// TODO Auto-generated method stub
		
	}
	public void insertParsingAsciiRejected(String filename, String exception, String callReff ,int lineNumber){
		PreparedStatement prepStmt = null;
        Connection conn;
        conn = SmsAppFactory.getDbConnection();

        String sql = bootParams.getQueryProperties().getProperty(PropertiesConstant.SQL_INSERT_CDR_REJECTED);
        try {
        	prepStmt = conn.prepareStatement(sql);
        	prepStmt.setString(1, filename);
        	prepStmt.setString(2, exception);
        	prepStmt.setString(3, callReff);
        	prepStmt.setInt(4, lineNumber);
        	prepStmt.executeUpdate();
        }catch(SQLException s) {
        	logger.error("Fail to insert core_cdr_rejected ",s);
        }finally {
        	try {
	        	prepStmt.close();
	        	conn.close();
        	}catch(SQLException s) {
        		logger.error("Error closing connection-finally block");
        	}
        }
        
        
        
	}
	public void insertParsingResultAscii(List parseResult, String filename) throws SQLException{
		// TODO Auto-generated method stub
        PreparedStatement prepStmt = null;
        Connection conn;
        conn = SmsAppFactory.getDbConnection();

        String sql = bootParams.getQueryProperties().getProperty(PropertiesConstant.SQL_INSERT_COMPTEL);
        prepStmt = conn.prepareStatement(sql);
        
        //conn.setAutoCommit(false);
        String callReff = "";
        int lineNum = 0;
        logger.info(Thread.currentThread().getName()+" Inserting "+parseResult.size()+" data");
        try {
        	
        	System.out.println(((OracleConnection)conn).getDefaultExecuteBatch());
        	((OracleConnection)conn).setDefaultExecuteBatch(10000);
	        for(int i=0;i<parseResult.size();i++) {
	        	FixedLengthAsciiBean f = (FixedLengthAsciiBean)parseResult.get(i);
	        		lineNum = i;
	        		if (f!=null) {
		        		if(callReff!=null) {
		        			callReff = f.getCallRef();
				        	prepStmt.setString(1, f.getCallRef());
				        	prepStmt.setString(2, filename);
				        	prepStmt.setString(3, f.getRecType());
				        	prepStmt.setString(4, f.getStructCode());
				        	prepStmt.setString(5, f.getClgNpi());
				        	prepStmt.setString(6, f.getClgNumber());
				        	prepStmt.setString(7, f.getClgCustId());
				        	prepStmt.setString(8, f.getCldNpi());
				        	prepStmt.setString(9, f.getCldNumber());
				        	prepStmt.setString(10, f.getCldCustId());
				        	prepStmt.setTimestamp(11, f.getMsgSubTime()==null?null:new java.sql.Timestamp(f.getMsgSubTime().getTime()));
				        	prepStmt.setTimestamp(12, f.getMsgDlrTime()==null?null:new java.sql.Timestamp(f.getMsgDlrTime().getTime()));
				        	prepStmt.setString(13, String.valueOf(f.getMsgStatus()));
				        	prepStmt.setString(14, f.getMsgLength());
				        	prepStmt.setString(15, f.getPriority());
				        	prepStmt.setString(16, f.getDeferred());
				        	prepStmt.setString(17, f.getReceipt());
				        	prepStmt.setString(18, f.getNbAttempt());
				        	prepStmt.setString(19, f.getProtId());
				        	prepStmt.setString(20, f.getClgMscFlag());
				        	prepStmt.setString(21, f.getClgMscGt());
				        	prepStmt.setString(22, f.getCldMscFlag());
				        	prepStmt.setString(23, f.getCldMscGt());
				        	prepStmt.setString(24, f.getMapError());
				        	prepStmt.setLong(25, 0);
				        	prepStmt.setLong(26, 0);
				        	prepStmt.setString(27, f.getClgImsi());
				        	prepStmt.setString(28, f.getCldImsi());
				        	prepStmt.setString(29, "");
				        	prepStmt.setString(30, f.getClgFlag());
				        	prepStmt.setString(31, f.getCldFlag());
				        	prepStmt.setString(32, f.getLocalError());
				        	
				        	prepStmt.addBatch();   
				        	//prepStmt.clearParameters();
			        	}
	        		}

	        	}
	        prepStmt.executeBatch();
	        	
	        	//conn.commit();
        	}catch(SQLException s) {
        		logger.error(s);
        		insertParsingAsciiRejected(filename, s.getMessage(), callReff, lineNum);
        	}finally {
        		logger.info(Thread.currentThread().getName()+"-Final-closing");
        		prepStmt.close();
        		conn.close();
        	}

	}
	
	public void insertParsingResultAsn1(List beanList, String filename) throws SQLException{
        PreparedStatement prepStmt = null;
        Connection conn;
        conn = SmsAppFactory.getDbConnection();

        String sql = bootParams.getQueryProperties().getProperty(PropertiesConstant.SQL_INSERT_LOGICA);
        prepStmt = conn.prepareStatement(sql);
        conn.setAutoCommit(false);
        
        logger.info("Inserting "+beanList.size()+" data");
        try {
	        for(int i=0;i<beanList.size();i++) {
	        	Asn1Bean f = (Asn1Bean)beanList.get(i);
		        	
		        	prepStmt.setString(1, filename);
		        	prepStmt.setString(2, f.getOrigAddress());
		        	prepStmt.setString(3, f.getOrigAddressTon());
		        	prepStmt.setString(4, f.getOrigAddressNpi());
		        	prepStmt.setString(5, f.getOrigAddressPid());
		        	prepStmt.setString(6, f.getOrigAddressMsisdn());
		        	prepStmt.setString(7, f.getOrigAddressGSM());
		        	prepStmt.setString(8, f.getRecipAddress());
		        	prepStmt.setString(9, f.getRecipAddressTon());
		        	prepStmt.setString(10, f.getRecipAddressNpi());
		        	prepStmt.setString(11, f.getRecipAddressPid());
		        	prepStmt.setString(12, f.getRecipAddressMsisdn());
		        	prepStmt.setString(13, f.getRecipAddressGSM());
		        	prepStmt.setString(14, f.getSubmitDate());
		        	prepStmt.setString(15, f.getSubmitTime());
		        	prepStmt.setString(16, f.getStatus());
		        	prepStmt.setString(17, f.getTerminDate());
		        	prepStmt.setString(18, f.getTerminTime());
		        	prepStmt.setString(19, f.getLengthOfMessage());
		        	prepStmt.setString(20, f.getPrioIndicator());
		        	prepStmt.setString(21, f.getValidityPeriod());
		        	prepStmt.setString(22, f.getValidPeriodHours());
		        	prepStmt.setString(23, f.getValidPeriodMinutes());
		        	prepStmt.setString(24, f.getDeferIndicator());
		        	prepStmt.setString(25, f.getDeferPeriod());
		        	prepStmt.setString(26, f.getNotifIndicator());
		        	prepStmt.setString(27, f.getNotifAddress());
		        	prepStmt.setString(28, f.getNotifAddressTon());
		        	prepStmt.setString(29, f.getNotifAddressNpi());
	
		        	prepStmt.setString(30, f.getNotifAddressPid());
		        	prepStmt.setString(31, f.getNotifAddressMsisdn());
		        	prepStmt.setString(32, f.getNotifAddressGSM());
		        	prepStmt.setString(33, f.getVsmscid());
		        	prepStmt.setString(34, f.getVsmsctype());
		        	prepStmt.setString(35, f.getDgtiAddress());
		        	prepStmt.setString(36, f.getDgtiAddressGSM());
		        	prepStmt.setString(37, f.getDestPointCode());
		        	prepStmt.setString(38, f.getOgtiAddress());
		        	prepStmt.setString(39, f.getOgtiAddressGSM());
		        	prepStmt.setString(40, f.getOrigPointCode());
		        	prepStmt.setString(41, f.getOrglSubmitDate());
		        	prepStmt.setString(42, f.getOrglSubmitTime());
		        	prepStmt.setString(43, f.getTransparentPid());
		        
		        	prepStmt.setString(44, f.getMesgReplyPath());
		        	prepStmt.setString(45, f.getRecipIntlMobileSubId());
		        	prepStmt.setString(46, f.getCallingLineId());
		        	prepStmt.setString(47, f.getCallingLineIdTon());
		        	prepStmt.setString(48, f.getCallingLineIdNpi());
		        	prepStmt.setString(49, f.getCallingLineIdPid());
		        	prepStmt.setString(50, f.getCallingLineIdMsisdn());
		        	prepStmt.setString(51, f.getCallingLineIdGSM());
		        	prepStmt.setString(52, f.getConsolidation());
		        	prepStmt.setString(53, f.getPortNumber());
		        	prepStmt.setString(54, f.getAser());
		        	prepStmt.setString(55, f.getMser());
		        	prepStmt.setString(56, f.getNser());
		        	prepStmt.setString(57, f.getXser());
		        	prepStmt.setString(58, f.getOrigIntlMobileSubId());
		        	prepStmt.setString(59, f.getBillid());
		        	prepStmt.setString(60, f.getLang());
		        	
		        	prepStmt.setString(61, f.getCbat());
		        	prepStmt.setString(62, f.getPpPser());
		        	prepStmt.setString(63, f.getPpAser());
		        	prepStmt.setString(64, f.getPpAserDuringJam());
		        	prepStmt.setString(65, f.getPpAserFree());
		        	prepStmt.setString(66, f.getPpAserRecip());
		        	prepStmt.setString(67, f.getSmeReference());
		        	prepStmt.setString(68, f.getSmsContentDcs());
		        	prepStmt.setString(69, f.getSmsContents());
		        	prepStmt.setString(70, f.getCmReferenceNr());
		        	prepStmt.setString(71, f.getCurrentSegment());
		        	prepStmt.setString(72, f.getSegmentsTotal());
		        	prepStmt.setString(73, f.getTextFormatting());
		        	prepStmt.setString(74, f.getBytesCompressedData());
		        	prepStmt.setString(75, f.getPredefinedAnimations());
		        	
		        	prepStmt.setString(76, f.getUserDefinedAnimations());
		        	prepStmt.setString(77, f.getPredefinedSounds());
		        	prepStmt.setString(78, f.getUserDefinedSounds());
		        	prepStmt.setString(79, f.getBlackWhitePictures());
		        	prepStmt.setString(80, f.getStandardWvg());
		        	prepStmt.setString(81, f.getCharacterSizeWvg());
		        	prepStmt.setString(82, f.getGreyscalePictures());
		        	prepStmt.setString(83, f.getColourPictures());
		        	prepStmt.setString(84, f.getBusinessCards());
		        	prepStmt.setString(85, f.getCalendarEntries());
		        	prepStmt.setString(86, f.getPolyphonicMelodi());
		        	prepStmt.setString(87, f.getBit8PortNumberDest());
		        	prepStmt.setString(88, f.getBit16PortNumberDest());
		        	prepStmt.setString(89, f.getLmsgNrSeg());
		        	prepStmt.setString(90, f.getMessageReference());
		        	
		        	prepStmt.setString(91, f.getBoolSer());
		        	prepStmt.setString(92, f.getOrigLASN());
		        	prepStmt.setString(93, f.getRecipLASN());
		        	prepStmt.setString(94, f.getOrigMsgID());
		        	prepStmt.setString(95, f.getRecipMsgID());
		        	prepStmt.setString(96, f.getReceiptDate());
		        	prepStmt.setString(97, f.getReceiptTime());
		        	prepStmt.setString(98, f.getIsr());
		        	prepStmt.setString(99, f.getRecipAltAddress());
		        	prepStmt.setString(100, f.getRecipAltAddressTon());
		        	prepStmt.setString(101, f.getRecipAltAddressNpi());
		        	prepStmt.setString(102, f.getRecipAltAddressPid());
		        	prepStmt.setString(103, f.getRecipAltAddressMsisdn());
		        	prepStmt.setString(104, f.getGeneratedSegments());
		        	
		        	prepStmt.setString(105, f.getServiceType());
		        	prepStmt.setString(106, f.getDeliveryAttempts());
		        	prepStmt.setString(107, f.getUntranslOrigAddress());
		        	prepStmt.setString(108, f.getUntranslOrigAddressGSM());
		        	prepStmt.setString(109, f.getUntranslRecipAddress());
		        	prepStmt.setString(110, f.getUntranslRecipAddressGSM());
		        	prepStmt.setString(111, f.getMsgError());
		        	prepStmt.setString(112, f.getTpDCS());
		        	prepStmt.setString(113, f.getGenericUrgencyLevel());
		        	prepStmt.setString(114, f.getIfaceUrgencyLevel());
		        	prepStmt.setString(115, f.getTeleserviceId());
		        	prepStmt.setString(116, f.getOrigAddrGroup());
		        	prepStmt.setString(117, f.getRecipAddrGroup());
		        	prepStmt.setString(118, f.getOrigNetworkType());
		        	prepStmt.setString(119, f.getRecipNetworkType());
		        	prepStmt.setString(120, f.getCldFlag());
		        	prepStmt.setString(121, f.getClgFlag());
		        	prepStmt.setString(122, f.getDgtiTon());
		        	prepStmt.setString(123, f.getDgtiPid());
		        	prepStmt.setString(124, f.getDgtiNpi());
		        	prepStmt.setString(125, f.getDgtiMsisdn());
		        	
		        	prepStmt.executeUpdate();   
		        	prepStmt.clearParameters();
		        	}
        	conn.commit();
        }catch(SQLException s) {
        	logger.error(s);
        }finally {
        	logger.info("finalle-closing");
        	prepStmt.close();
        	conn.close();
        }
	}
}
